home *** CD-ROM | disk | FTP | other *** search
/ Fritz: All Fritz / All Fritz.zip / All Fritz / FILES / PROGNG_C / TURBOCU2.LZH / RMTREE.C < prev    next >
Text File  |  1987-09-05  |  3KB  |  153 lines

  1. #define TRUE 1
  2. #define FALSE 0
  3.  
  4. #include <stdio.h>
  5. #include <conio.h>
  6. #include <dir.h>
  7. #include <dos.h>
  8. #include <io.h>
  9.  
  10. main(argc, argv)
  11. int argc;
  12. char *argv[];
  13.  
  14. {
  15.  
  16.     if (argc != 2) {
  17.         puts("usage: rmtree directory_name to remove a directory tree recursively.");
  18.         puts("\nchdir to the parent directory of directory_name to use rmtree.");
  19.         puts("rmtree will delete *all* files and directories including directory_name.");
  20.         puts("hidden, system and r/o files will be deleted after confirmation.");
  21.         puts("\nTHIS PROGRAM SHOULD BE USED WITH CAUTION!\007\n");
  22.         _exit();
  23.     }
  24.  
  25.     if ((pos('?',argv[1]) > -1) || (pos('*',argv[1]) > -1)) {
  26.         puts("Wildcards not allowed.");
  27.         _exit();
  28.     }
  29.  
  30.     printf("\nAbout to remove tree %s. Are you sure? ", argv[1]);
  31.     if (getconfirm())
  32.         scandir(argv[1]);
  33.     else puts("\nAborted.");
  34. }
  35.  
  36. /*
  37. **    scan a directory
  38. */
  39. scandir(filemask)
  40. char *filemask;
  41.  
  42. {
  43.     int done;
  44.     struct ffblk ffblk;
  45.     char dirname[80], localmask[80];
  46.  
  47.     done = findfirst(filemask, &ffblk, 0x3f);
  48.     if (done)
  49.         perror("rmtree");
  50.     while (!done) {
  51.         if (ffblk.ff_attrib == 16) {        /* we found another directory */
  52.             if (ffblk.ff_name[0] != '.') {    /* spare this one and parent  */
  53.                 strcpy(dirname, ffblk.ff_name);
  54.                 if (chdir(dirname)) {        /* try to chdir for recursion */
  55.                     printf("rmtree: can't chdir to %s.\n", dirname);
  56.                     _exit();
  57.                 };
  58.                 strcpy(localmask, "*.*");    /* now get *all* files and .. */
  59.                 scandir(localmask);            /* play it again, sam!        */
  60.                 chdir("..");                /* back from recursion, now ..*/
  61.                 if (rmdir(dirname)) {        /* we remove the emptied dir  */
  62.                     perror("rmtree");
  63.                     _exit();
  64.                 }
  65.             }
  66.         }
  67.         else {  /* here we got a normal file, so we just unlink it */
  68.             do_delete(ffblk.ff_name, ffblk.ff_attrib);
  69.         }
  70.         done = findnext(&ffblk);            /* get next dir-entry til done */
  71.     }
  72. }
  73.  
  74. /*
  75. **    delete a file, give user a chance to abort
  76. */
  77.  
  78. do_delete(fname, fattr)
  79. char *fname;
  80. int fattr;
  81.  
  82. {
  83.     if (fattr & FA_RDONLY) {
  84.         printf("%s read only. continue? ", fname);
  85.         if (!getconfirm())
  86.             _exit();
  87.         else {
  88.             _chmod(fname, 1, 0);
  89.             fattr = 0;
  90.         }
  91.     }
  92.     if (fattr & FA_HIDDEN) {
  93.         printf("%s hidden file. continue? ", fname);
  94.         if (!getconfirm())
  95.             _exit();
  96.         else {
  97.             _chmod(fname, 1, 0);
  98.             fattr = 0;
  99.         }
  100.     }
  101.  
  102.     if (fattr & FA_SYSTEM) {
  103.         printf("%s system file. continue? ", fname);
  104.         if (!getconfirm())
  105.             _exit();
  106.         else {
  107.             _chmod(fname, 1, 0);
  108.             fattr = 0;
  109.         }
  110.     }
  111.     unlink(fname);
  112. }
  113.  
  114. /*
  115. **    get user's confirmation for anything
  116. */
  117.  
  118. int getconfirm()
  119.  
  120. {
  121.     char ch;
  122.  
  123.     do {
  124.         ch = toupper(getch());
  125.         if ((ch == '\x1b') || (ch == '\x03')) {
  126.             puts("\nAborted.");
  127.             _exit();
  128.         }
  129.     } while ((ch != 'N') && (ch != 'Y'));
  130.     putchar(ch);
  131.     putchar('\n');
  132.     return(ch == 'Y');
  133.  
  134. }
  135. /*
  136.  *  find a given character in a given string          *************************
  137.  */
  138.  
  139. int pos (ch,string)
  140.  
  141. char ch;
  142. char string[];
  143.  
  144. {
  145.     int i;
  146.  
  147.     for (i=0;string[i] != '\0'; i++)
  148.         if (string[i] == ch)
  149.             return(i);
  150.     return (-1);
  151.  
  152. }
  153.